package controller.ur.note;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import model.dao.PhotoDaoLocal;
import model.entity.Note;
import model.entity.Photo;

import com.google.gson.Gson;

/**
 * Servlet implementation class SNoteGet
 */
public class SNoteGet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	@EJB
	private PhotoDaoLocal photoDao;
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SNoteGet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String pids=(String)request.getParameter("photoId");
		Photo photo=photoDao.findById(Integer.parseInt(pids));
		
		if(photo!=null){
			response.setContentType("application/json");
			response.setCharacterEncoding("UTF-8");
			PrintWriter writer=response.getWriter();
			writer.write("[");
			
			int i=0;
			for(Note note:photo.getNotes()){
				writer.write("{");
				writer.write("\"top\": "+note.getX()+",");
				writer.write("\"left\":"+ note.getY()+",");
				writer.write("\"width\":"+ note.getWidth()+",");
				writer.write("\"height\":"+ note.getHeight()+",");
				writer.write("\"text\":\""+ note.getCaption()+"\",");
				writer.write("\"id\":\""+ note.getId().toString()+"\",");
				writer.write("\"editable\":"+ "true");
			    writer.write("}");
			    i++;
			    if(i!=photo.getNotes().size()){
			    	writer.write(",");
			    }
			}
			
			writer.write("]");
		    response.getWriter().close();
		}else{
			response.getWriter().close();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request,response);
	}
	

}
